﻿Imports ISNet.WebUI.WebTreeView
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports ISNet.WebUI.WebGrid
Imports System.Data
Imports log4net

Partial Class Report_R01
    Inherits System.Web.UI.Page

    Public ScriptText As String = ""
    Private Shared logger As ILog = LogManager.GetLogger("Report_R01")

    Protected Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
        WebCombo3.EncodeOutput = True
    End Sub
    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        'Session("users") = "bb0011"
        'Session("PlanYear") = "2557"

        If Session("users") Is Nothing Then
            Response.Redirect("LoginForm.aspx")
        End If
        
        Label1.Text = "ระบบจัดทำงบประมาณ >> รายงาน >> ภาพรวมสถานะการจัดทำงบประมาณ"
        If IsPostBack = False Then
            Try
                Dim adap As New DBdataTableAdapters.AppConfigsTableAdapter
                Dim dt As New DBdata.AppConfigsDataTable
                dt = adap.GetData
                For Each item As DBdata.AppConfigsRow In dt
                    Select Case item.Name
                        Case "ACTIVE_PLAN_YEAR"
                            PlanYearsTxt.Text = item.Value
                    End Select
                Next
            Catch ex As Exception
                logger.Error(ex.Message)
                logger.Error(ex.StackTrace)
            End Try
        End If


        'Label1.Text = Date.Now.AddYears(543).Year
        'PlotNode()
    End Sub

    Sub PlotNode()

        Dim MyNode As WebTreeViewNode
        MyNode = New WebTreeViewNode()

        Dim adapter As New FundCenterParentTableAdapters.fctr_viewTableAdapter
        Dim dt As New FundCenterParent.fctr_viewDataTable
        dt = adapter.GetDataByUser(Session("users"), Val(PlanYearsTxt.Text))

        Dim root_id As String

        MyNode.Text = "GSB"
        MyNode.Value = "0"
        root_id = "0"

        If dt.Count > 0 Then

            WebTreeView3.Nodes.Add(MyNode)

            Dim rootpath = ""
            rootpath = FctrClass.GetRoot(Session("users"), Val(Session("planyear")))
            Dim rootarr() As String = Split(rootpath, ",")
            WebTreeView3.Nodes.Add(MyNode)

            For i = 0 To rootarr.Length - 1
                PlotChild(rootarr(i), MyNode, 1)
            Next
        End If

    End Sub

    Sub PlotChild(ByVal gpath As String, ByRef cnode As WebTreeViewNode, ByVal level As Decimal)
        'Response.Write(gpath & "<br>")
        Dim Hadapter As New FundCenterParentTableAdapters.FctHTableAdapter
        Dim Hdatatable As New FundCenterParent.FctHDataTable
        Hdatatable = Hadapter.GetDataByGPath(gpath, Val(Session("planyear")))

        Dim MyNode As WebTreeViewNode
        MyNode = New WebTreeViewNode()
        For Each row As FundCenterParent.FctHRow In Hdatatable


            MyNode.Text = row.Description
            MyNode.Value = row.FundCenter_FundCenterCode

            cnode.Nodes.Add(MyNode)
        Next


        If Len(gpath) >= 10 Then
            Exit Sub
        End If

        Dim hadap2 As New FundCenterParentTableAdapters.UserLikeGpathTableAdapter
        Dim hdt2 As New FundCenterParent.UserLikeGpathDataTable
        Dim sql As String = ""
        Dim prefil As String = ""
        prefil = ""

        Dim pathadd As String = "__"
        'For i = 1 To 4
        sql = ""
        Dim fullpath As String = gpath & pathadd
        If prefil <> "" Then
            sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
        End If
        sql += " AND grouppath like '" & fullpath & "' "
        sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND Fcth.PlanYear = " & Val(Session("PlanYear"))
        hadap2.GetDataByFilter(hdt2, sql)

        For Each row As FundCenterParent.UserLikeGpathRow In hdt2
            If Len(row.grouppath) <= 10 Then
                PlotChild(row.grouppath, MyNode, level + 1)
            End If

        Next

    End Sub


    'Protected Sub WebCombo1_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo1.InitializeDataSource

    '    'If Session("Versions") > 1 Then
    '    'ds = PublicFunction.GetChild2(Session("users"))
    '    'ds.Tables(0).DefaultView.Sort = "FundCenterCode ASC"
    '    'e.DataSource = ds
    '    Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
    '    Dim dt As New BasicData.FCTR_Child2DataTable
    '    Dim sql = ""
    '    Dim prefil As String = ""
    '    'prefil = Session("prefilter")
    '    sql += " WHERE 1 = 1 "
    '    sql += " AND UserName = '" & Session("users") & "'"
    '    'If prefil <> "" Then
    '    '    sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
    '    'End If
    '    sql += "ORDER BY FundCenterCode ASC"
    '    adap.GetDataByFilter(dt, sql)
    '    dt.Rows.Add("", "ทุกรายการ", "1", "0")
    '    dt.DefaultView.Sort = "FundCenterCode"
    '    e.DataSource = dt



    'End Sub

    'Protected Sub WebCombo2_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo2.InitializeDataSource


    '    Dim adap As New BasicDataTableAdapters.FCTR_Child2TableAdapter
    '    Dim dt As New BasicData.FCTR_Child2DataTable
    '    Dim sql = ""
    '    Dim prefil As String = ""
    '    'prefil = Session("prefilter")
    '    sql += " WHERE 1 = 1 "
    '    sql += " AND UserName = '" & Session("users") & "'"

    '    sql += "ORDER BY FundCenterCode ASC"
    '    adap.GetDataByFilter(dt, sql)
    '    dt.Rows.Add("", "ทุกรายการ", "1", "0")
    '    dt.DefaultView.Sort = "FundCenterCode"
    '    e.DataSource = dt

    'End Sub


    Protected Sub toReport_Click(sender As Object, e As ImageClickEventArgs) Handles toReport.Click
        If Val(PlanYearsTxt.Text) = 0 Then
            ScriptText = "<script language='javascript'>alert('กรุณาระบุปีงบประมาณ');</script>"
            Exit Sub
        End If

        Session.Remove("year")
        Session.Add("year", PlanYearsTxt.Text)


        Session.Remove("FundcenterAll")


        Session.Remove("FCTRmode")
        Session.Add("FCTRmode", "2")

        'Add by Orn 28/4/2558
        Dim useradap As New UserDSTableAdapters.UsersTableAdapter()
        Dim userDS As New UserDS.UsersDataTable
        userDS = useradap.GetDataByUsername(Session("users"))
        Dim enableAllUser As Boolean = False
        If userDS.Rows.Count > 0 Then
            If userDS(0)("Enable_All_Report_Flag").ToString() <> "" Then
                enableAllUser = userDS(0)("Enable_All_Report_Flag")
            End If
        End If
        'End Add by Orn 28/4/2558

        If WebCombo3.Value.ToString = "0" Or WebCombo3.Value.ToString = "" Then
            If enableAllUser = True Then 'Add by Orn 28/4/2558
                Session.Add("FundcenterAll", "0000")
            Else
                Dim topadap As New Data_ReportTableAdapters.FctHTableAdapter
                Dim topfctr As String = ""
                Try
                    topfctr = topadap.GetFctr(Session("users"), Val(PlanYearsTxt.Text))
                Catch ex As Exception
                    logger.Error(ex.Message)
                    logger.Error(ex.StackTrace)
                End Try

                If Val(Session("RoleType")) = 1 Or Val(Session("RoleType")) = 5 Then
                    topfctr = "0000"
                End If

                Session.Add("FundcenterAll", topfctr)
            End If
        Else

            Session.Add("FundcenterAll", WebCombo3.Value.ToString)
        End If

        'If Val(WebCombo3.Value) <> 0 Then
        'Session.Add("FundcenterAll", FctrClass.GetSubFundCenter(WebCombo3.Value, Session("users"), Val(Session("PlanYear")), ""))
        'Else
        '    Session.Add("FundcenterAll", FctrClass.GetUserFundCenter(Session("users"), Val(Session("planyear"))))
        'End If

        Session.Remove("VersionStatus")
        Session.Add("VersionStatus", DDL_Status.SelectedValue)


        ScriptText = "<script language='javascript'>openpop();</script>"


    End Sub


    Protected Sub WebCombo3_InitializeDataSource(sender As Object, e As ISNet.WebUI.WebCombo.DataSourceEventArgs) Handles WebCombo3.InitializeDataSource

        Dim adap As New FundCenterParentTableAdapters.SelectorTableAdapter
        Dim dt As New FundCenterParent.SelectorDataTable
        Dim dt2 As New FundCenterParent.SelectorDataTable
        Dim sql = ""
        Dim prefil As String = ""
        Dim enableAllReport As Boolean = False
        enableAllReport = Session("EnableAllReport")
        prefil = Session("prefilter")
        If enableAllReport Then
            sql += " WHERE 1 = 1 "
            sql += " AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FctH.PlanYear = " & Val(Session("PlanYear"))
            sql += " GROUP BY  FctH.Description, FctH.FundCenter_FundCenterCode, FctH.ParentFundCenterHierarchy_Id"
            sql += ", FctH.grouppath,  FctH.pathname, FctH.planyear "
            sql += "ORDER BY Grouppath"
        Else
            sql += " WHERE 1 = 1 "
            sql += " and USERNAME = '" & Session("users") & "' AND fctr_view.PlanYear = " & Val(Session("PlanYear")) & " AND FctH.PlanYear = " & Val(Session("PlanYear"))
            If prefil <> "" Then
                sql += " AND fctr_view.FundCenterCode IN (" & prefil & ") "
            End If
            sql += "ORDER BY Grouppath"
        End If
        
        adap.GetDataByFilter(dt2, sql)
        dt.Rows.Add("ทุกรายการ", "0", 0, "0", Val(Session("PlanYear")), "ทุกรายการ")
        dt.Merge(dt2)
        'dt.DefaultView.Sort = "FundCenter_FundCenterCode"
        e.DataSource = dt



    End Sub

End Class
